package com.carhome.ui.fagment;

import com.carhome.R;
import com.carhome.adapter.CarFragmentSearchAdapter;
import com.carhome.adapter.SearchAdapter;
import com.carhome.bean.CarSearchCondition;
import com.carhome.ui.base.BaseFragment;
import com.carhome.utils.CarsBrandLists;

import android.graphics.Rect;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager.LayoutParams;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.FrameLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

public class CarFragmentByCondition extends BaseFragment {
	// 界面上的控件
	private ListView mListView;
	private TextView mTextView;

	private CarFragmentSearchAdapter mAdapter;

	private PopupWindow mPopupWindow;

	// 解析布局
	private View mView;
	private View mPopView;

	private int width;
	private int height;

	private LayoutInflater inflater; // 保存布局解析器
	int statusBarHeight;

	FrameLayout popContainer; // popwindows中需要填充的控件
	SearchAdapter searchAdapter; // 搜索条件设置listview的adapter
	ListView conditionList; // 显示搜索条件的listview

	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		mView = inflater.inflate(R.layout.car_fragment_bycondition, container, false);
		this.inflater = inflater;
		initViews();
		return mView;
	}

	private void initViews() {
		mListView = (ListView) mView.findViewById(R.id.car_fragment_bycondition_listview);
		mTextView = (TextView) mView.findViewById(R.id.car_fragment_bycondition_textview);
		mTextView.setOnClickListener(myClickListener);
		mListView.setOnItemClickListener(mylistviewItemClickListener);

		mAdapter = new CarFragmentSearchAdapter(getActivity());
		mAdapter.addData(new CarSearchCondition("品牌", "不限"));
		mAdapter.addData(new CarSearchCondition("价格", "不限"));
		mAdapter.addData(new CarSearchCondition("级别", "不限"));
		mAdapter.addData(new CarSearchCondition("国别", "不限"));
		mAdapter.addData(new CarSearchCondition("变速箱", "不限"));
		mAdapter.addData(new CarSearchCondition("结构", "不限"));
		mAdapter.addData(new CarSearchCondition("排量", "不限"));
		mAdapter.addData(new CarSearchCondition("燃料", "不限"));
		mAdapter.addData(new CarSearchCondition("配置", "不限"));

		mListView.setAdapter(mAdapter);
		width = getResources().getDisplayMetrics().widthPixels / 7 * 5;
		height = getResources().getDisplayMetrics().heightPixels;

		mPopView = inflater.inflate(R.layout.search_popwin, null, false);
		popContainer = (FrameLayout) mPopView.findViewById(R.id.search_pop_contanier);
		statusBarHeight = getStateBarHeight();
		mPopView.setOnTouchListener(myTouchListener); // 让popwindows在点击其他地方消失
		mPopupWindow = new PopupWindow(mPopView, width, LayoutParams.MATCH_PARENT, true);
		mPopupWindow.setAnimationStyle(R.style.AnimationFade); // 设置进入/退出动画

		searchAdapter = new SearchAdapter(getActivity());
		conditionList = new ListView(getActivity());
	}

	private OnItemClickListener mylistviewItemClickListener = new OnItemClickListener() {

		public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
			switch (position) {
			case 0: // 品牌
				CarsBrandLists carsBrandLists = null;
				if (carsBrandLists == null) {
					carsBrandLists = new CarsBrandLists(getActivity());
					View inflaterview = carsBrandLists.createViewFromLayout(inflater);
					popContainer.removeAllViews();
					popContainer.addView(inflaterview);
					TextView popTitle = (TextView) mPopView.findViewById(R.id.search_pop_title);
					popTitle.setText("选择品牌");
				}
				mPopupWindow.showAtLocation(mListView, Gravity.RIGHT, 0, statusBarHeight);// 设置popwindows显示位置
				break;
			case 1: // 价格
				showinfoCondition(R.array.search_price);
				break;
			case 2:// 级别
				showinfoCondition(R.array.search_level);
				break;
			case 3:// 国别
				showinfoCondition(R.array.search_country);
				break;
			case 4:// 变速箱
				showinfoCondition(R.array.search_gearbox);
				break;
			case 5://结构
				showinfoCondition(R.array.search_structure);
				break;
			case 6://排量
				showinfoCondition(R.array.search_displacement);
				break;
			case 7:
				showinfoCondition(R.array.search_fuel);
				break;
			case 8:
				showinfoCondition(R.array.search_equirements);
				break;
			default:
				break;
			}
		}

		private void showinfoCondition(int id) {
			popContainer.removeAllViews();
			searchAdapter.removeAllData();
			searchAdapter.addDataFromArrayId(id);
			conditionList.setAdapter(searchAdapter);
			popContainer.addView(conditionList);
			mPopupWindow.showAtLocation(mListView, Gravity.RIGHT, 0, statusBarHeight);// 设置popwindows显示位置
		}

	};

	private OnClickListener myClickListener = new OnClickListener() {
		public void onClick(View v) {
			// TODO
		}
	};

	/**
	 * 获取状态栏的高度
	 */
	public int getStateBarHeight() {
		Rect frame = new Rect();
		getActivity().getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);

		int statusBarHeight = frame.top;
		return statusBarHeight;
	}

	private OnTouchListener myTouchListener = new OnTouchListener() {

		public boolean onTouch(View v, MotionEvent event) {
			if (mPopupWindow != null && mPopupWindow.isShowing()) {
				mPopupWindow.dismiss();
			}
			return false;
		}
	};
}
